package demo03;

import java.util.HashMap;
import java.util.Map;
//统计数组中能被k整除的子数组
class Solution {
    public int subarraysDivByK(int[] nums, int k) {
        Map<Integer,Integer> hash =new HashMap<Integer,Integer>();
        int ret = 0;//统计数量
        int sum = 0;
        hash.put(0,1);//处理当前数组为目标数组的情况
        for(int num : nums){
            sum = sum + num;
            int rem = (sum%k+k) % k;
            ret = ret + hash.getOrDefault(rem,0);
            hash.put(rem,hash.getOrDefault(rem,0)+1);
        }
        return ret;
    }
}